// OGoMojo
// Whitemice.MOGI.ZOGI.TaskAnnotation
// License: LGPL
// Copyright: Whitemice Consulting, 2006
// Author(s): Adam Tauno Williams (awilliam@whitemice.org)
using System;
using Whitemice.MOGI;

namespace Whitemice.MOGI.ZOGI
{	
	public class TaskAnnotation : Document, ITaskAnnotation
	{
		public TaskAnnotation(Server server, Payload payload)
			: base(server, payload)
		{
		}
		
		public string Document
		{
			get { return "Document/Task/" + payload.GetStringForKey("taskObjectId"); }
		}
		
		public override string Name
		{
			get 
			{ 
				return "Annotation/Document/Task/" + 
					payload.GetStringForKey("taskObjectId"); 
			}
		}
		
		public string Comment
		{
			get { return payload.GetStringForKey("comment"); }
			set { payload.SetValueForKey("comment", value); }
		}
				
		public DateTime Date
		{
			get { return payload.GetDateTimeForKey("actionDate"); }
		}
		
		public string Actor
		{
			get 
			{
				return iserver.GetName(payload.GetStringForKey("actorObjectId"));
			}
		}
		
		public ETaskStatus Status
		{
			get { return GetStatus(payload.GetStringForKey("taskStatus")); }
		}		
		
		/// \brief Turns an action keys string into an ETaskStatus value
		/// \warn Only some of these statuses are actually useful
		protected ETaskStatus GetStatus(string key)
		{
	  	switch (payload.GetStringForKey("action")) 
	  	{
				case "00_created": return ETaskStatus.Created;
				case "02_rejected": return ETaskStatus.Rejected;
				case "05_accepted": return ETaskStatus.Accepted;
				case "10_archived": return ETaskStatus.Archived;
				case "10_commented": return ETaskStatus.Commented;
				case "15_divided": return ETaskStatus.Divided;
				case "20_processing": return ETaskStatus.Processing;
				case "25_done": return ETaskStatus.Done;
				case "27_reactivated": return ETaskStatus.Reactivated;
				case "30_archived": return ETaskStatus.Archived;
				default: return ETaskStatus.Created;
	  	}
	 	}
	 	
	 	protected void SetStatus(ETaskStatus status)
	 	{
			switch (status) 
		  {
	   		case ETaskStatus.Created: 
	   				payload.SetValueForKey("action", "00_created"); 
	   			break;
	   		case ETaskStatus.Rejected: 
	   				payload.SetValueForKey("action", "02_rejected"); 
	   			break;
	   		case ETaskStatus.Accepted: 
	   				payload.SetValueForKey("action"," 05_accepted"); 
	   			break;
	   		case ETaskStatus.Archived: 
	   				payload.SetValueForKey("action", "10_archived"); 
	   			break;
	   		case ETaskStatus.Commented: 
	   				payload.SetValueForKey("action", "10_commented");
	   			break;
	   		case ETaskStatus.Divided: 
	   				payload.SetValueForKey("action", "15_divided"); 
	   			break;
	   		case ETaskStatus.Processing: 
	   				payload.SetValueForKey("action", "20_processing"); 
	   			break;
	   		case ETaskStatus.Done: 
	   				payload.SetValueForKey("action", "25_done");	
	   			break;
	   		case ETaskStatus.Reactivated: 
	   				payload.SetValueForKey("action", "27_reactivated"); 
	   			break;
	  	}	 	
	 	}
	 	
	 	/// \brief Allow the type of annotation to be adjusted/retrieved
	 	public ETaskStatus Action
	 	{
	 		get { return GetStatus(payload.GetStringForKey("action")); }
		 	set { SetStatus(value); }
		}
	}
}
